package com.quasar.hpatient.view.adapter;

import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.R;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.litePalmodel.HospitalInfodb;
import com.quasar.hpatient.model.litePalmodel.PositonInfodb;
import com.quasar.hpatient.model.medicalmodel.DoctorListBean;
import com.quasar.hpatient.network.RetrofitConnection;
import com.quasar.hpatient.utils.Spanny;
import com.vise.log.ViseLog;

import java.util.List;

/**
 * author : 许建宏
 * Date: 2017/10/12 0012
 * View:所有医生
 */

public class DoctorFindAdapter extends BaseQuickAdapter<DoctorListBean, BaseViewHolder> {

    public DoctorFindAdapter() {
        super(R.layout.add_item_doctor);
    }

    List<HospitalInfodb> hospitalInfodbList = MainAppliaction.getInstance().getHospitalInfodbList();
    //科室
    List<PositonInfodb> positonInfodbList = MainAppliaction.getInstance().getPositonInfoList();

    @Override
    protected void convert(BaseViewHolder helper, DoctorListBean item) {
        ViseLog.d("enter");
        String name = "",//真实姓名
                position = "",//职称
                hospital = "",//医院
                goodAt = "";//擅长


        ViseLog.d("首字母的拼音"+"*******"+item.getIndex());

        TextView tv_index=helper.getView(R.id.tv_index);
        if(helper.getLayoutPosition()==0 || !DoctorFindAdapter.this.getData().get(helper.getLayoutPosition()-1).getIndex().equals(item.getIndex())){
            tv_index.setVisibility(View.VISIBLE);
            tv_index.setText(item.getIndex());
        }else{
            tv_index.setVisibility(View.GONE);
        }

        //真实姓名
        if (item.getRealName() != null && item.getRealName().length() > 0) {
            name = item.getRealName();
            ViseLog.d(item.getRealName());
        }else{
            name="匿名";
        }
        //职称
        if (item.getTitleId() != 0) {
            for (int i = 0; i < positonInfodbList.size(); i++) {
                if (item.getTitleId() == positonInfodbList.get(i).getPrimaryid()) {
                    position = positonInfodbList.get(i).getName();
                    break;
                }
            }
        }else{
            position="未知";
        }
        Spanny spanny = new Spanny();
        spanny.append(name + " ", new ForegroundColorSpan(color(R.color.headtextcolor)));
        spanny.append(position + " ", new ForegroundColorSpan(color(R.color.textcolor)));
        helper.setText(R.id.tv_name, spanny);

        //医院
        if (item.getHospitalId() != 0) {
            int size = hospitalInfodbList.size();
            for (int i = 0; i < size; i++) {
                if (item.getHospitalId() == hospitalInfodbList.get(i).getPrimaryid()) {
                    hospital = hospitalInfodbList.get(i).getName();
                    break;
                }
            }
        }else{
            hospital="未知";
        }
        helper.setText(R.id.tv_hospital, hospital);

        //擅长
        if (item.getRemark() != null && item.getRemark().length() != 0) {
            goodAt = item.getRemark();
        }else{
            goodAt="暂无";
        }
        Spanny spa = new Spanny();
        spa.append("擅长：", new ForegroundColorSpan(color(R.color.btcolor)));
        spa.append(goodAt + " ", new ForegroundColorSpan(color(R.color.textcolor)));
        helper.setText(R.id.tv_good, spa);
        TextView tv_apply = helper.getView(R.id.tv_apply);
        RelativeLayout relativeLayout=helper.getView(R.id.rl_examine);

        if (item.getDoctorPatientRelative() != null) {
            //判断医生状态
            ViseLog.d("医生的状态"+item.getDoctorPatientRelative().getRelationType() );
//            if (item.getDoctorPatientRelative().getRelationType() != 0) {
                if(item.getDoctorPatientRelative().getRelationType()==ConstantManger.Relation1){
                    relativeLayout.setVisibility(View.VISIBLE);
                    tv_apply.setText("取消");
                }else if(item.getDoctorPatientRelative().getRelationType()== ConstantManger.Relation2
                        || item.getDoctorPatientRelative().getRelationType()==ConstantManger.Relation4
                        || item.getDoctorPatientRelative().getRelationType()==ConstantManger.stopRelation){
                    tv_apply.setText("取消");
                    relativeLayout.setVisibility(View.GONE);
                }else{
                    tv_apply.setText("申请");
                    tv_apply.setVisibility(View.VISIBLE);
                    relativeLayout.setVisibility(View.GONE);
                }
//            }else{
//                tv_apply.setVisibility(View.VISIBLE);
//                relativeLayout.setVisibility(View.GONE);
//            }
        }else {
            relativeLayout.setVisibility(View.GONE);
        }

        //头像
        if (item.getCover() != null && item.getCover().length() > 0) {
            Glide.with(mContext).load(RetrofitConnection.PHOTOURL + item.getCover()).into((ImageView) helper.getView(R.id.headimg));
        }else{
            Glide.with(mContext).load(R.mipmap.doctor).into((ImageView) helper.getView(R.id.headimg));
        }
        helper.addOnClickListener(R.id.tv_apply);
        helper.addOnClickListener(R.id.headimg);
    }

    private int color(int value) {
        return MainAppliaction.getInstance().getResources().getColor(value);
    }
}
